home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part2 / 15155 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  1.2 KB

  1. Path: news.umbc.edu!not-for-mail
  2. From: schlein@umbc.edu (Jonas J. Schlein)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: A weird thing about printf()
  5. Date: 17 Apr 1996 11:49:23 -0400
  6. Organization: University of Maryland Baltimore County
  7. Message-ID: <4l33u3$6vt@umbc9.umbc.edu>
  8. References: <4kflr2$5if@dewey.csun.edu>
  9. NNTP-Posting-Host: umbc9.umbc.edu
  10. NNTP-Posting-User: schlein
  11.  
  12. |>   The program :
  13. |> 
  14. |> #include <stdio.h>
  15. |> 
  16. |> int answer;
  17. |> main()
  18. |> {
  19. |>   answer=2+2;
  20. |>   printf("The answer is %d\n");
  21.  
  22. You never tell printf() the variable you want to print! Change this line to:
  23.  
  24. printf("The answer is %d\n", answer);
  25.  
  26. |>   return 0;
  27. |> }
  28. |> 
  29. |>   In printf(),I lost "answer" in the end,but it works,and give me the
  30. |> result 0.I wonder how the compiler handle this condition.Also,is this
  31. |> allow by C?
  32.  
  33. Undefined behavior means anything can happen. Including even getting lucky
  34. and having it print what you expected.
  35.  
  36. |> (by this, I mean is this a leagal usage in language itself,
  37. |> or just a mistake cause by the compiler?)
  38.  
  39. It's certainly not the way printf() was intended to work. If you give it a 
  40. %d it expects an int further along in the argument list.
  41. -- 
  42. "If it wasn't for C, we would be using BASI, PASAL, and OBOL."
  43.  
  44. Jonas J. Schlein  (schlein@gl.umbc.edu)
  45.